package modelo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import modelo.Status;
import modelo.Tarea;

/*
 * Maneja entidades:
 *   - Tarea
 */

public class DAOTareas {
	protected HashMap<Integer, Tarea> tareas = new HashMap<Integer, Tarea>();
	
	public void cargarTareas() {
		ResultSet rs = Servidor.consultar(
			"SELECT *\n" +
			"FROM tarea\n"
			);
		
		try {
			while (rs.next()) {
				Tarea e = new Tarea(rs.getInt("cod_tarea"), rs.getString("descripcion"), rs.getDouble("costo"), Status.valueOf(rs.getInt("status")));
				tareas.put(e.clave(), e);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<Tarea> consultarTareas() {
		ResultSet rs = Servidor.consultar(
			"SELECT cod_tarea\n" +
			"FROM tarea\n"
			);
		
		return adjuntarTarea(rs);
	}

	public Tarea consultarTareaPorCodigo(int codigo) {
		return tareas.get(codigo);
	}
	
	private ArrayList<Tarea> adjuntarTarea(ResultSet rs) {
		ArrayList<Tarea> lst = new ArrayList<Tarea>();
		
		try {
			while (rs.next())
				lst.add(tareas.get(rs.getInt("cod_tarea")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lst;
	}
	
}
